package demo4;


class Animal {
    public String name;
    public int age;

    public void eat() {
        System.out.println(this.name + "eat()! Animal");
    }

//    public void sleep() {
//        System.out.println(this.name + "sleep()!");
//    }
}

class Cat extends Animal {

    public String fur;

    @Override
    public void eat() {
        System.out.println(this.name + "吃猫粮!");
    }

    //    public void eat() {
//        System.out.println(this.name + "吃猫粮!");
//    }

    public void mew() {
        System.out.println(this.name + "cat::mew()!");
    }
}

class Dog extends Animal {

    public void bark() {
        System.out.println(this.name + "dog::bark()!");
    }

    @Override
    public void eat() {
        System.out.println(this.name + "吃狗粮!");
    }
}

public class TestDemo {

    public static void main(String[] args) {
        Animal animal = new Cat();
        Cat cat = (Cat) animal; // 向下转型
        cat.mew();
    }

    // 多态
    public static void function(Animal animal) {
        animal.eat();
    }

    public static void main4(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();

        function(cat);
        function(dog);

    }

    public static void main3(String[] args) {
        Animal animal = new Cat();
        animal.name = "咪咪";
        animal.eat();
    }

    /**
     * 向上转型三种方式：
     * 1、直接赋值
     * 2、传参
     * 3、返回值
     */
    public static void func(Animal animal) {
        // 传参
    }

    public static Animal func2() {
        return new Cat(); // 返回值
    }

    public static void main2(String[] args) {
        Cat cat = new Cat();
        func(cat);
        func(new Cat());
    }

    public static void main1(String[] args) {
        Animal animal = new Cat(); // 向上转型,直接赋值
        animal.name = "咪咪";
        animal.eat();
        //animal.mew(); // 不能访问子类的
    }
}
